package _17_graph;

import java.util.List;
import java.util.StringJoiner;

/**
 * @author: mornd
 * @dateTime: 2023/6/21 - 20:46
 * 顶点定义
 */
public class Vertex {
    String name;
    List<Edge> edges;

    // 该顶点是否被访问过，深度优先遍历使用
    boolean visited = false;

    // 记录顶点的入度，拓扑排序使用
    int inDegree;

    // 访问状态 0-未访问 1-访问中 2-访问过 拓扑排序使用
    int status = 0;

    // 距离，默认无穷大
    int dist = Integer.MAX_VALUE;

    // 上一个
    Vertex prev;

    public Vertex(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    // Java 表示图，参照 img/构建图.png
    public static void main(String[] args) {
        // 构建顶点
        Vertex v1 = new Vertex("1");
        Vertex v2 = new Vertex("2");
        Vertex v3 = new Vertex("3");
        Vertex v4 = new Vertex("4");
        Vertex v5 = new Vertex("5");
        Vertex v6 = new Vertex("6");

        // 建立边的关系
        v1.edges = List.of(new Edge(v3, 9),
                new Edge(v2, 7),
                new Edge(v6, 14));

        v2.edges = List.of(new Edge(v4, 15));

        v3.edges = List.of(new Edge(v4, 11),
                new Edge(v6, 2));

        v4.edges = List.of(new Edge(v5, 6));

        v6.edges = List.of(new Edge(v5, 9));
        v5.edges = List.of();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Vertex)) return false;

        Vertex vertex = (Vertex) o;

        return name != null ? name.equals(vertex.name) : vertex.name == null;
    }

    @Override
    public int hashCode() {
        return name != null ? name.hashCode() : 0;
    }

    //    @Override
//    public String toString() {
//        return new StringJoiner(", ", "{", "}")
//                .add("name='" + name + "'")
//                .add("dist=" + dist)
//                .toString();
//    }


    @Override
    public String toString() {
        return new StringJoiner(", ", "[", "]")
                .add("name='" + name + "'")
                .toString();
    }
}
